﻿using System;
using System.Collections.Generic;
using csvm.recordbuilder.contract;
using csvm.datamodel.contract;
using System.Linq;


namespace csvm.recordbuilder
{
    public class RecordBuilder : IRecordBuilder
    {
        public Record Header { get; private set; }

        public void ProcessBuildRecords(IEnumerable<string> lines) {
            OnGetHeader(GetHeader(lines));
            OnBuildPages(BuildRecords(lines));
        }

        private static Record GetHeader(IEnumerable<string> lines) {
            return new Record(SplitLineIntoColumns(lines.First()));
        }

        private static IEnumerable<Record> BuildRecords(IEnumerable<string> lines) {
            return lines.Select(line => new Record(SplitLineIntoColumns(line)));
        }

        private static IEnumerable<string> SplitLineIntoColumns(string line) {
            return line.Split(';');
        }

        public event Action<Record> OnGetHeader;
        public event Action<IEnumerable<Record>> OnBuildPages;
    }
}
